package cn.yanghx.pay.process.wechat;

import cn.yanghx.pay.conf.property.PayProperties;
import cn.yanghx.pay.constant.PayConstants;
import cn.yanghx.pay.exception.WechatAppPayException;
import cn.yanghx.pay.feign.WechatAppPayFeign;
import cn.yanghx.pay.process.PayProcessInterface;
import cn.yanghx.pay.vo.feign.wechat.app.close.WechatAppCloseResult;
import cn.yanghx.pay.vo.feign.wechat.app.close.WechatAppPayCloseParams;
import cn.yanghx.pay.vo.feign.wechat.app.prepay.WechatAppPrepayParams;
import cn.yanghx.pay.vo.feign.wechat.app.prepay.WechatAppPrepayResult;
import cn.yanghx.pay.vo.feign.wechat.app.query.WechatAppQueryResult;
import cn.yanghx.pay.vo.pay.prepay.PrePayParams;
import feign.Response;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 微信app支付
 *
 * @author yanghx
 */
@Component
public class WechatAppPayProcessImpl implements PayProcessInterface {




    @Override
    public boolean match(PayConstants.PayMethodEnum payMethodEnum) {
        return PayConstants.PayMethodEnum.wechat_app_pay == payMethodEnum;
    }

    @Resource
    private WechatAppPayFeign wechatAppPayFeign;
    @Resource
    private PayProperties payProperties;


    @Override
    public WechatAppPrepayResult prepay(PrePayParams params) {
        //限定实体类
        if (!(params instanceof WechatAppPrepayParams)) {
            throw new WechatAppPayException("解析参数信息失败");
        }
        WechatAppPrepayParams wechatAppPrepayParams = (WechatAppPrepayParams) params;
        return wechatAppPayFeign.prepay(wechatAppPrepayParams);
    }

    @Override
    public WechatAppQueryResult queryForTransactionId(String transactionId) {
        String mchId = payProperties.getWechat().getApp().getMchId();
        return wechatAppPayFeign.queryForWechatOrderId(transactionId, mchId);
    }

    @Override
    public WechatAppQueryResult queryForOutTradeNo(String outTradeNo) {
        String mchId = payProperties.getWechat().getApp().getMchId();
        return wechatAppPayFeign.queryForOutTradeNo(outTradeNo, mchId);
    }

    @Override
    public WechatAppCloseResult close(String outTradeNo) {
        String mchId = payProperties.getWechat().getApp().getMchId();

        WechatAppPayCloseParams params = new WechatAppPayCloseParams();
        params.setMchid(mchId);
        Response response = wechatAppPayFeign.close(outTradeNo, params);
        WechatAppCloseResult result = new WechatAppCloseResult();
        result.setSuccess(204 == response.status());
        return result;


    }


}
